<template>
  <div>
    <h2>用户列表</h2>
    <el-button type="success" size="mini" @click="goAdd" plain>添加用户</el-button>
    <el-table :data="tableData">
      <el-table-column prop="id" label="序号"> </el-table-column>
      <el-table-column prop="username" label="名称"> </el-table-column>
      <el-table-column prop="gender" label="性别"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="email" label="电子邮箱"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <template>
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
      </div>
    </template>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1
    }
  },
  methods: {
    // 删除按钮
    handleDelete() {},
    // 编辑按钮
    handleEdit(id) {
      this.$router.push({ path: '/dashboard/user/mod', query: { id: id } })
    },
    // 改变每页显示的数据数量
    handleSizeChange(val) {
      this.$http.get('/api/getusers?page=' + this.pageNum).then((res) => {
        if (res.errNo === 0 && res.message === 'success') {
          this.tableData = res.paginate.data
          // 调整表格的长度
          for (let i = 0; i < 10 - val; i++) {
            this.tableData.pop()
          }
          // this.total = res.paginate.total
        }
        // 调整gander为中文
        this.tableData.forEach((item) => {
          if (item.gender === '1') {
            item.gender = '男'
          } else if (item.gender === '2') {
            item.gender = '女'
          } else {
            item.gender = '保密'
          }
        })
        console.log(this.tableData)
      })
    },
    // 页码点击事件
    handleCurrentChange(val) {
      this.pageNum = val
      this.$http.get('/api/getusers?page=' + this.pageNum).then((res) => {
        if (res.errNo === 0 && res.message === 'success') {
          this.tableData = res.paginate.data
          this.total = res.paginate.total
        }
        // 调整gander为中文
        this.tableData.forEach((item) => {
          if (item.gender === '1') {
            item.gender = '男'
          } else if (item.gender === '2') {
            item.gender = '女'
          } else {
            item.gender = '保密'
          }
        })
        console.log(this.tableData)
      })
    },
    goAdd() {
      this.$router.push('/dashboard/user/add')
    }
  },
  created() {
    // 获取数据列表
    this.handleCurrentChange()
  }
}
</script>

<style>
.el-pagination {
  padding: 10px 300px;
}
h2 {
  display: inline-block;
  margin: 10px;
}
</style>
